package com.idis.service;

import com.idis.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ming on 2018/1/25.
 */

/**
 * 登录用户服务
 * 主要用于实现特定接口，否则登录系统无法正常执行
 */
@Service
public class UserSecuriryService implements UserDetailsService{

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    String DBUrl;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println(DBUrl+"/user/username/"+s);
        List<User> user;
        user = restTemplate.exchange(DBUrl+"/user/username/"+s, HttpMethod.GET, null, new ParameterizedTypeReference<ArrayList<User>>(){}).getBody();
        if(user.size()==0){
            return new User();
        }
        return user.get(0);
    }
}
